package seqlist.oj;

/**
 * @Author: yuisama
 * @Date: 2019/12/6 16:43
 * @Description:移除链表中所有元素
 */
public class Solution203 {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }
        if (head == null) {
            return null;
        }else {
            // prev为待删除节点的前驱
            ListNode prev = head;
            while (prev.next != null) {
                if (prev.next.val == val) {
                    prev.next = prev.next.next;
                }else {
                    prev = prev.next;
                }
            }
            return head;
        }
    }
}
